package com.jy.adscm.operation.purchase.service.impl;

import com.alibaba.druid.filter.encoding.EncodingConvertFilter;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.operation.config.MyPage;
import com.jy.adscm.operation.purchase.bean.Bomandsupplier;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.bean.QueryByPageParameter;
import com.jy.adscm.operation.purchase.mapper.mysql.BomandsupplierMapper;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.operation.purchase.service.BomandsupplierService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * bom和供应商管理配置表 服务实现类
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@Service
public class BomandsupplierServiceImpl extends ServiceImpl<BomandsupplierMapper, Bomandsupplier> implements BomandsupplierService {
@Autowired
    BomandsupplierMapper bomandsupplierMapper;
@Autowired
    PurchasebomMapper purchasebomMapper;
@Autowired
    JyCodeMapper jyCodeMapper;
    @Override
    public MyPage<Bomandsupplier> query(QueryByPageParameter queryByPageParameter) {
        MyPage<Bomandsupplier> myPage=new MyPage<>();
        if (queryByPageParameter.getPage()!=0){
            if (queryByPageParameter.getPage()==1){
                queryByPageParameter.setPage(0);
            }else {
                queryByPageParameter.setPage((queryByPageParameter.getPage()-1)*queryByPageParameter.getRows());
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryByPageParameter.getSortBy(),null)){
            queryByPageParameter.setSortBy(queryByPageParameter.getSortBy()+(queryByPageParameter.getDesc() ? " DESC":" ASC"));
        }
        List<Bomandsupplier> query = bomandsupplierMapper.query(queryByPageParameter.getPage(),queryByPageParameter.getRows()
                                                                ,queryByPageParameter.getKey(),queryByPageParameter.getSortBy());
        for (Bomandsupplier bomandsupplier : query) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(bomandsupplier.getUse(),null)){
                Wrapper<JyCode> wrapper=new EntityWrapper<>();
                wrapper.eq("code_type",3).eq("code",bomandsupplier.getUse());
                List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCodes,null)&&jyCodes.size()>0){
                    bomandsupplier.setUseName(jyCodes.get(0).getName());
                }
            }
        }
        Integer integer = bomandsupplierMapper.queryCount(queryByPageParameter.getKey());
        myPage.setItems(query);
        myPage.setTotal(integer);
        return myPage;
    }

    @Override
    public List<Purchasebom> queryByBom() {
        List<Purchasebom> purchaseboms = purchasebomMapper.queryByBom();
        for (Purchasebom purchasebom : purchaseboms) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(purchasebom.getSpecification(),null)){
                purchasebom.setBomName(purchasebom.getBomName()+"("+purchasebom.getSpecification()+")");
            }

        }
        return purchaseboms;
    }
}
